package com.platform.api.film.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.platform.api.bzproject.model.BzAllWallpaper;
import com.platform.api.bzproject.model.BzTalent;
import com.platform.api.bzproject.service.BzAllWallpaperService;
import com.platform.api.bzproject.service.IBzTalentService;

import com.platform.api.film.model.FilmPlayRecord;
import com.platform.api.film.model.FilmShort;
import com.platform.api.film.query.MovieVo;
import com.platform.api.film.service.FilmPlayRecordService;
import com.platform.common.core.Result;

import com.platform.common.core.ResultCode;
import com.platform.common.util.PageParam;
import com.platform.security.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author guoyunfei on 2023/9/3 11:02
 * @className MovieController
 * @description TODO 影视资源
 * @Version 1.0
 */
@RestController
@RequestMapping("{appid}/film/movie")
public class MovieController {


    @Autowired
    private BzAllWallpaperService bzAllWallpaperService;

    @Autowired
    private FilmPlayRecordService filmPlayRecordService;

    /**
     * 分页获取
     */
    @GetMapping("u/page")
    public Result page(Long filmId, Integer status,PageParam<BzAllWallpaper> page) {
        IPage<BzAllWallpaper> wallpaperIPage = bzAllWallpaperService.page(page, new LambdaQueryWrapper<BzAllWallpaper>()
                .eq(BzAllWallpaper::getOpenid,SecurityUtils.getUser().getUserId())
                        .eq(BzAllWallpaper::getIsDelete,1)
                        .eq(Objects.nonNull(filmId),BzAllWallpaper::getFilmId,filmId)
                        .eq(Objects.nonNull(status),BzAllWallpaper::getStatus,status)
                        .select(BzAllWallpaper::getId,BzAllWallpaper::getThumbnail,BzAllWallpaper::getStatus,
                                BzAllWallpaper::getTitle,BzAllWallpaper::getNumber)
                .orderByDesc(BzAllWallpaper::getId));
        return Result.success(wallpaperIPage);
    }
    @PostMapping("u/saveResource")
    public Result saveWallpaper(@RequestBody BzAllWallpaper bzAllWallpaper)  {
        return Result.bool(bzAllWallpaperService.saveResource(bzAllWallpaper));
    }

    @GetMapping("u/getDetail/{id}")
    public Result getDetail(@PathVariable Long id)  {
        return Result.success(bzAllWallpaperService.getDetail(id));
    }

    /**
     * 定位资源,按照type值进行不同查询条件
     * @return
     */
    @GetMapping("u/position")
    public Result position(MovieVo movieVo)  {
        FilmPlayRecord lastRecord = filmPlayRecordService.getLastRecord(movieVo.getFilmId());
        movieVo.setResourceId(lastRecord.getResourceId());
        return Result.success(bzAllWallpaperService.get(movieVo));
    }


}
